package com.heima.article.controller.v1;

import com.alibaba.fastjson.JSON;
import com.heima.article.pojo.ApArticle;
import com.heima.article.service.ApArticleService;
import com.heima.common.constants.message.ArticleBehaviorRedisPrefixConstants;
import com.heima.common.dtos.ArticleCommentPageDto;
import com.heima.common.dtos.ResponseResult;
import com.heima.common.util.JsonUtils;
import com.heima.model.article.dtos.ArticleDto;
import com.heima.model.article.dtos.ArticleHomeDto;
import com.heima.model.article.dtos.ArticleInfoDto;
import com.heima.model.behavior.dtos.ArticleBehaviorShowDTO;
import com.heima.model.comment.dtos.CommentPageDto;
import com.heima.model.media.dtos.WmNewsResultDTO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Fuqiang
 * @since :2023-03-27
 */
@RestController
@RequestMapping("/api/v1/article")
public class ApArticleController {

    @Autowired
    ApArticleService articleService;

    @Autowired
    StringRedisTemplate redisTemplate;

    /**
     * 查询列表详情，用于feign调用
     *
     * @param dto
     * @return
     */
    @GetMapping("/api/v1/article/findComment")
    public List<CommentPageDto> findByPage(@RequestBody ArticleCommentPageDto dto) {
        return articleService.findComment(dto);
    }


    /**
     * 回显用户行为数据
     *
     * @param dto
     * @return
     */
    @PostMapping("/load_article_behavior")
    public ResponseResult<ArticleBehaviorShowDTO> loadArticleBehavior(@Validated @RequestBody ArticleInfoDto dto) {

        return ResponseResult.ok(articleService.loadArticleBehavior(dto));
    }

    @GetMapping("/test/{userId}/{articleId}")
    public String testUpRedis(@PathVariable String articleId, @PathVariable String userId) {

        String key = ArticleBehaviorRedisPrefixConstants.BEHAVIOR_SHOW_PREFIX + ":" + userId;


        // 第一步 嘗試從Redis中讀取數據
        String json = (String) redisTemplate.opsForHash().get(key, articleId + "");

        if (StringUtils.isEmpty(json) || JsonUtils.toBean(json, ArticleBehaviorShowDTO.class) == null) {

            ArticleBehaviorShowDTO dto = new ArticleBehaviorShowDTO();
            dto.setIscollection(true);
            redisTemplate.opsForHash().put(key, articleId + "", JSON.toJSONString(dto));
            return "ok";
        } else {
            ArticleBehaviorShowDTO dto = JsonUtils.toBean(json, ArticleBehaviorShowDTO.class);
            if (dto != null) {

                dto.setIscollection(true);
                redisTemplate.opsForHash().put(key, articleId + "", JSON.toJSONString(dto));
            }
        }


        ArticleBehaviorShowDTO dto = new ArticleBehaviorShowDTO();
        dto.setIsfollow(true);
        dto.setIslike(true);
        dto.setIscollection(true);
        dto.setIsunlike(false);
        redisTemplate.opsForHash().put(key, articleId + "", JSON.toJSONString(dto));
        return "ok";
    }

    /**
     * 创建文章信息 用于Feign调用
     *
     * @param dto
     * @return
     */
    @PostMapping("/save")
    public Long createArticle(@Validated @RequestBody WmNewsResultDTO dto) {
        return articleService.createArticle(dto);
    }

    /**
     * 加载app首页文章
     *
     * @param dto
     * @return
     */
    @PostMapping("/load")
    public ResponseResult<List<ApArticle>> load(@Validated @RequestBody ArticleHomeDto dto) {
        List<ApArticle> apArticles = articleService.load(dto);
        return ResponseResult.ok(apArticles);
    }

    /**
     * 加载app更多文章
     *
     * @param dto
     * @return
     */
    @PostMapping("/loadmore")
    public ResponseResult<List<ApArticle>> loadMore(@Validated @RequestBody ArticleHomeDto dto) {
        List<ApArticle> apArticles = articleService.loadMore(dto);
        return ResponseResult.ok(apArticles);
    }

    /**
     * 加载app最新文章
     *
     * @param dto
     * @return
     */
    @PostMapping("/loadnew")
    public ResponseResult<List<ApArticle>> loadNew(@Validated @RequestBody ArticleHomeDto dto) {
        List<ApArticle> apArticles = articleService.loadNew(dto);
        return ResponseResult.ok(apArticles);
    }


    /**
     * feign调用 分页查询文章以及文章详情信息 用于全量导入es
     *
     * @param page 页码
     * @param size 页面大小
     * @return
     */
    @GetMapping("/findByPage")
    public List<ArticleDto> findByPage(@RequestParam("page") Integer page, @RequestParam("size") Integer size) {
        return articleService.findByPage(page, size);
    }


    /**
     * feign远程调用 ,根据articleId查询article文章以及文章详情记录
     *
     * @param articleId
     * @return
     */
    @GetMapping("/findById/{id}")
    public ArticleDto findById(@PathVariable("id") Long articleId) {
        return articleService.findById(articleId);
    }
}
